/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.rails.extjs.form;

import java.util.ArrayList;
import java.util.List;

import org.rails.extjs.Component;
import org.rails.extjs.Option;

public class CheckboxGroup extends Field {

	private List<Checkbox> checkboxs = new ArrayList<Checkbox>();
	
	public CheckboxGroup() {
		super();
	}
	
	public CheckboxGroup(int columns) {
		super();
		setColumns(columns);
	}

	public CheckboxGroup(Option configOptions) {
		super(configOptions);
	}

	public CheckboxGroup(int columns,String fieldLabel) {
		super();
		setFieldLabel(fieldLabel);
		setColumns(columns);
	}

	public void addCheckbox(Checkbox checkbox){
		checkboxs.add(checkbox);		
	}
	
	public List<Checkbox> getCheckboxs(){
		return checkboxs;
	}
	
	public void removeCheckbox(int index){
		checkboxs.remove(index);
	}
	
	public Component getCheckbox(int index){
		return checkboxs.get(index);
	}
	
	public void clearCheckbox(){
		checkboxs.clear();
	}
	public void setAllowBlank(Boolean allowBlank){
		addOption("allowBlank", allowBlank);
	}
	
	public Boolean isAllowBlank(){
		return getBoolean("allowBlank",true);
	}

	public void setBlankText(String blankText){
		addOption("blankText", blankText);
	}

	public String getBlankText(){
		return getString("blankText","You must select at least one item in this group");
	}
	
	public void setColumns(int columns){
		addOption("columns", columns);
	}
	
	public int getColumns(){
		return getInteger("columns",0);
	}
	
	public void setVertical(Boolean vertical){
		addOption("vertical", vertical);
	}
	
	public Boolean isVertical(){
		return getBoolean("vertical",false);
	}
	
	
	@Override
	public String getScript() {
		script = new StringBuffer("");
		createItems();
		addOption("items",checkboxs);
		script.append("new Ext.form.CheckboxGroup(");
		script.append(createOptions());
		script.append(")");
		return script.toString();
	}	

}
